//
//  StatusCell.swift
//  sinaweibo
//
//  Created by mac on 15/11/14.
//  Copyright © 2015年 mac. All rights reserved.
//

import UIKit
import SnapKit

let statusCellMargin:CGFloat = 13
let statusIconWidth:CGFloat = 35

class StatusCell: UITableViewCell {

    var bottomConstraints: Constraint?
    
    var status:Status? {
        didSet{
        //原创微博赋值
        originalView.status = status
        //转发微博赋值
            self.bottomConstraints?.uninstall()
            if let re = status?.retweeted_status {
                
                reweetedView.reweetedStatus = re
                reweetedView.hidden = false
                
                bottomView.snp_updateConstraints(closure: { (make) -> Void in
                    self.bottomConstraints = make.top.equalTo(reweetedView.snp_bottom).constraint
                })
            }else{
                reweetedView.hidden = true
                bottomView.snp_updateConstraints(closure: { (make) -> Void in
                    self.bottomConstraints = make.top.equalTo(originalView.snp_bottom).constraint
                })
            }
        }
    }
    
    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        //设置cell的组样式
        selectionStyle = .None
        setupUI()
        
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    //设置ui界面
    private func setupUI() {
        contentView.addSubview(originalView)
        contentView.addSubview(bottomView)
        contentView.addSubview(reweetedView)
        //设置约束
        originalView.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(contentView.snp_top)
            make.left.equalTo(contentView.snp_left)
            make.right.equalTo(contentView.snp_right)
//            make.height.equalTo(50)
        }
        
        reweetedView.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(originalView.snp_bottom) //.offset(statusCellMargin)
            make.left.equalTo(originalView.snp_left)
            make.right.equalTo(originalView.snp_right)
//            make.bottom.equalTo(100)
        }
        
        bottomView.snp_makeConstraints { (make) -> Void in
            make.left.equalTo(contentView.snp_left)
            make.right.equalTo(contentView.snp_right)
            self.bottomConstraints = make.top.equalTo(reweetedView.snp_bottom).constraint
            make.height.equalTo(40)
        }
        
        contentView.snp_makeConstraints { (make) -> Void in
//            make.bottom.equalTo(originalView.snp_bottom)
            make.bottom.equalTo(bottomView.snp_bottom)
            make.top.equalTo(self.snp_top)
            make.left.equalTo(self.snp_left)
            make.right.equalTo(self.snp_right)
        }
    }
    
    //懒加载原创微博视图
    private lazy var originalView:StatusOriginalView = StatusOriginalView()
    
    //懒加载底部视图
    private lazy var bottomView:StatusCellBottomView = StatusCellBottomView()
    
    private lazy var reweetedView:ReweetedStatusView = ReweetedStatusView()
    
}
